package com.ruoyi.web.controller.weather;

import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.common.response.Result;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.inspection.service.LocationService;
import com.ruoyi.inspection.service.WeatherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/weather")
public class WeatherController {
    @Autowired
    private WeatherService weatherService;
    @Autowired
    private LocationService locationService;

    /**
     * 天气指数预报
     */
    @GetMapping("/heFengTianQi/indices/forecast")
    public Result indicesForecast(String address) throws Exception {
        address = StringUtils.isEmpty(address) ? "浙江省杭州市余杭区余杭街道梅家桥星苑" : address;
        String location = locationService.getLongitudeLatitude(address);
        JSONObject result = weatherService.indicesForecastOneDay(location, "0");
        return Result.success(result);
    }

    /**
     * 格点逐小时天气预报
     */
    @GetMapping("/heFengTianQi/gridWeather/24h")
    public Result gridWeather24h(String address) throws Exception {
        address = StringUtils.isEmpty(address) ? "浙江省杭州市余杭区余杭街道梅家桥星苑" : address;
        String location = locationService.getLongitudeLatitude(address);
        JSONObject result = weatherService.gridWeather24h(location);
        return Result.success(result);
    }

    /**
     * 每日天气预报 3天预报
     */
    @GetMapping("/heFengTianQi/weather/3d")
    public Result weather3d(String address) throws Exception {
        if (StringUtils.isEmpty(address)) {
            return Result.success();
        }
//        address = StringUtils.isEmpty(address) ? "浙江省杭州市余杭区余杭街道梅家桥星苑" : address;
        String location = locationService.getLongitudeLatitude(address);
        JSONObject result = weatherService.weather3d(location);
        return Result.success(result);
    }
}
